// 权限控制
// 1. 路由跳转权限控制
// 2. 菜单权限控制

// console.log('权限控制生效了')
import router from './router'
import store from '@/store'
import { getToken } from './utils/auth'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
const WHITELIST = ['/login', '/404']
router.beforeEach((to, from, next) => {
  NProgress.start()
  const token = getToken()
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.state.user.userInfo.userId) {
        store.dispatch('user/fetchUserInfo')
      }
      next()
    }
  } else {
    if (WHITELIST.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
  NProgress.done()
})
